{include file="public/header" type='pear-container'/}
<style>
    .layui-table-tool-panel{
        display: none;
    }
</style>
<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
                           
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">卡券代码</label>
                   <div class="layui-input-inline">
                       <input type="text" name="code" placeholder="" class="layui-input">
                   </div>
               </div>
               <div class="layui-form-item layui-inline">
                   <label class="layui-form-label">卡券类型</label>
                   <div class="layui-input-inline">
                      <select name="type">
                           <option value="">请选择一个类型</option>
                           <option value="1">余额充值卡</option>
                           <option value="2">会员兑换卡</option>
                       </select>  
                   </div>
               </div>
                        <div class="layui-form-item layui-inline">
                       <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="query">
                           <i class="layui-icon layui-icon-search"></i>
                           查询
                       </button>
                       <button type="reset" class="pear-btn pear-btn-md">
                           <i class="layui-icon layui-icon-refresh"></i>
                           重置
                       </button>
                       </div>
                    </div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="cdk" lay-filter="cdk"></table>
			</div>
		</div>

		<script type="text/html" id="toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        添加卡券
			</button>
			<button class="pear-btn pear-btn-warming pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="allRemove">
 			    <i class="layui-icon layui-icon-delete"></i>
 			    清理已使用卡券
 		    </button>
		</script>


		<script type="text/html" id="options">
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
		{include file="public/footer"/}
        <script>
        layui.use(['table', 'form', 'jquery','excel'], function() {
            let table = layui.table;
            let excel = layui.excel;
            let form = layui.form;
            let $ = layui.jquery;
            let MODULE_PATH = "{$Request.root}/ypay.shop/";
            
            let cols = [
                [{
                        type: 'checkbox'
                    }, {
                       field: "id",
                       title: "ID",
                       unresize: "true",
                       align: "center"
                   },{
                       field: "type",
                       title: "卡券类型",
                       unresize: "true",
                       width:200,
                       align: "center",
                       templet: function (d) {
                            switch(d.type){
                                case 1:
                                    return '<span style="color:#1e9fff;">余额卡</span>';
                                break;
                                case 2:
                                    return '<span style="color:#ffb800;">会员卡</span>';
                                break;
                            }
                       }
                   }, {
                       field: "code",
                       title: "卡券代码",
                       unresize: "true",
                       align: "center"
                   }, {
                       field: "value",
                       title: "卡券面值",
                       unresize: "true",
                       align: "center",
                       templet: function (d) {
                            switch(d.type){
                                case 1:
                                    return d.value + '元';
                                break;
                            }
                            return d.value;
                       }
                   }, {
                       field: "create_time",
                       title: "创建时间",
                       unresize: "true",
                       align: "center"
                   }, {
                       field: "status",
                       title: "状态",
                       unresize: "true",
                       align: "center",
                       templet: function (d) {
                            switch(d.status){
                                case 0:
                                    return '<span class="layui-badge pear-btn-success pear-btn-xs">未使用</span>';
                                break;
                                case 1:
                                    return '<span class="layui-badge pear-btn-danger pear-btn-xs">已使用</span>';
                                break;
                            }
                       }
                   },{
                        title: '操作',
                        toolbar: '#options',
                        unresize: true,
                        align: 'center',
                        width:180,
                    }
                ]
            ]

            table.render({
                elem: '#cdk',
                id:'cdk',
                url: MODULE_PATH + 'cdk',
                page: true,
                cols: cols,
                cellMinWidth: 100,
                skin: 'line',
                toolbar: '#toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
            
            table.on('tool(cdk)', function(obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                }
            });

            table.on('toolbar(cdk)', function(obj) {
                if (obj.event === 'add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'batchRemove') {
                    window.batchRemove(obj);
                }else if (obj.event === 'allRemove') {
 					window.allRemove(obj);
 				}else if (obj.event === 'LAYTABLE_EXPORT') {
                
                // 获取表格中的所有数据
                var AllData = table.cache['cdk'];
                console.log(AllData);
                // 导出选中的数据
                var exportData = [];
                layui.each(AllData, function (index, item) {
                    exportData += item.code + '\n';
                });
                // 去除最后一行的空格
                exportData = exportData.trim();
                // 导出所有数据
                if (exportData.length > 0) {
                    var blob = new Blob([exportData], { type: 'text/plain;charset=utf-8' });
                    saveAs(blob, 'cdk.txt');
                } else {
                  layer.msg('表格没有数据');
                }
              }
            });

            form.on('submit(query)', function(data) {
                table.reload('cdk', {
                    where: data.field,
                    page:{curr: 1}
                })
                
                return false;
            });
            //弹出窗设置 自己设置弹出百分比
            function screen() {
                if (typeof width !== 'number' || width === 0) {
                width = $(window).width() * 0.6;
                }
                if (typeof height !== 'number' || height === 0) {
                height = $(window).height() * 0.6;
                }
                return [width + 'px', height + 'px'];
            }

            window.add = function() {
                layer.open({
                    type: 2,
                    maxmin: true,
                    title: '添加卡券',
                    shade: 0.1,
                    area: screen(),
                    content: MODULE_PATH + 'add_cdk'
                });
            }

            window.remove = function(obj) {
                layer.confirm('确定要删除该Cdk嘛', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'removeCdk',
                        data:{id:obj.data['id']},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.batchRemove = function(obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                var ids = []
                var hasCheck = table.checkStatus('cdk')
                var hasCheckData = hasCheck.data
                if (hasCheckData.length > 0) {
                    $.each(hasCheckData, function (index, element) {
                        ids.push(element.id)
                    })
                }
                layer.confirm('确定要删除这些Cdk嘛', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url:MODULE_PATH + 'batchRemoveCdk',
                        data:{ids:ids},
                        dataType: 'json',
                        type: 'POST',
                        success: function(res) {
                            layer.close(loading);
                            //判断有没有权限
                            if(res && res.code==999){
                                layer.msg(res.msg, {
                                    icon: 5,
                                    time: 2000, 
                                })
                                return false;
                            }else if (res.code==200) {
                                layer.msg(res.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('cdk');
                                });
                            } else {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
            
            window.allRemove = function(obj) {
 				layer.confirm('确定要清理所有已使用卡券嘛', {
 					icon: 3,
 					title: '提示'
 				}, function(index) {
 					layer.close(index);
 					let loading = layer.load();
 					$.ajax({
 						url: MODULE_PATH + 'allRemoveCdk',
 						type: 'POST',
 						success: function(res) {
 							layer.close(loading);
 							//判断有没有权限
 							if (res && res.code == 999) {
 								layer.msg(res.msg, {
 									icon: 5,
 									time: 2000,
 								})
 								return false;
 							} else if (res.code == 200) {
 								layer.msg(res.msg, {
 									icon: 1,
 									time: 1000
 								}, function() {
 									table.reload('cdk');
 								});
 							} else {
 								layer.msg(res.msg, {
 									icon: 2,
 									time: 1000
 								});
 							}
 						}
 					})
 				});
 			}

            window.refresh = function(param) {
                table.reload('cdk');
            }
        })
    </script>
	</body>
</html>
